package com.leo.blog.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dao.BlackIpDao;
import com.leo.blog.dto.BlackIpExecution;
import com.leo.blog.entity.BlackIp;
import com.leo.blog.enums.BlackIpStateEnum;
import com.leo.blog.exceptions.BlackIpOperationException;
import com.leo.blog.service.BlackIpService;
import com.leo.blog.util.ConstantUtil;
import com.leo.blog.util.PageCalculator;

@Service
public class BlackIpServiceImpl implements BlackIpService{
	@Autowired
	private BlackIpDao blackDao;

	@Override
	@SystemLog(description = ConstantUtil.BACKIP_ADD,userType=ConstantUtil.USERTYPE_ADMIN)
	public BlackIpExecution addBlackIp(String name, BlackIp blackIp) throws BlackIpOperationException {
		if(blackIp != null) {
			//初始化创建时间
			blackIp.setCreateTime(new Date());
			
			int effectedNum = blackDao.insertBlackIp(blackIp);
			if(effectedNum > 0) {
				return new BlackIpExecution(BlackIpStateEnum.SUCCESS);
			}else {
				return new BlackIpExecution(BlackIpStateEnum.OFFLINE);
			}
		}else {
			return new BlackIpExecution(BlackIpStateEnum.NULL_BLACKIP);
		}
	}

	@Override
	public BlackIpExecution addSelective(BlackIp blackIp) throws BlackIpOperationException {
		if(blackIp != null) {
			//初始化创建时间
			blackIp.setCreateTime(new Date());
			
			int effectedNum = blackDao.insertSelective(blackIp);
			if(effectedNum > 0) {
				return new BlackIpExecution(BlackIpStateEnum.SUCCESS);
			}else {
				return new BlackIpExecution(BlackIpStateEnum.OFFLINE);
			}
		}else {
			return new BlackIpExecution(BlackIpStateEnum.NULL_BLACKIP);
		}
	}

	@Override
	public BlackIpExecution getBlackIpById(int blackId) {
		BlackIpExecution be = new BlackIpExecution();
		if(blackId > 0) {
			BlackIp black = blackDao.queryBlackIpById(blackId);
			if(black != null) {
				be.setBlackIp(black);
				be.setState(BlackIpStateEnum.SUCCESS.getState());
			}else {
				be.setState(BlackIpStateEnum.OFFLINE.getState());
			}
		}else {
			be.setState(BlackIpStateEnum.OFFLINE.getState());
		}
		return be;
	}

	@Override
	public BlackIpExecution getBlackIpByIp(String blackIp) {
		BlackIpExecution be = new BlackIpExecution();
		if(blackIp != null) {
			BlackIp black = blackDao.queryBlackIpByIp(blackIp);
			if(black != null) {
				be.setBlackIp(black);
				be.setState(BlackIpStateEnum.SUCCESS.getState());
			}else {
				be.setState(BlackIpStateEnum.OFFLINE.getState());
			}
		}else {
			be.setState(BlackIpStateEnum.OFFLINE.getState());
		}
		return be;
	}

	@Override
	public BlackIpExecution updateBlackIpById(BlackIp blackIp) {
		if(blackIp != null) {
			int effectedNum = blackDao.updateBlackIpById(blackIp);
			if(effectedNum > 0) {
				return new BlackIpExecution(BlackIpStateEnum.SUCCESS);
			}else {
				return new BlackIpExecution(BlackIpStateEnum.OFFLINE);
			}
		}else {
			return new BlackIpExecution(BlackIpStateEnum.NULL_BLACKIP);
		}
	}

	@Override
	public BlackIpExecution updateBlackIpByIpSelective(BlackIp blackIp) {
		if(blackIp != null) {
			int effectedNum = blackDao.updateBlackIpByIpSelective(blackIp);
			if(effectedNum > 0) {
				return new BlackIpExecution(BlackIpStateEnum.SUCCESS);
			}else {
				return new BlackIpExecution(BlackIpStateEnum.OFFLINE);
			}
		}else {
			return new BlackIpExecution(BlackIpStateEnum.NULL_BLACKIP);
		}
	}

	@Override
	@SystemLog(description = ConstantUtil.BACKIP_DELETE,userType=ConstantUtil.USERTYPE_ADMIN) 
	public BlackIpExecution deleteBlackIpByVid(String name, int blackVid) {
		if(blackVid > 0) {
			int effectedNum = blackDao.deleteBlackIpByVid(blackVid);
			if(effectedNum > 0) {
				return new BlackIpExecution(BlackIpStateEnum.SUCCESS);
			}else {
				return new BlackIpExecution(BlackIpStateEnum.OFFLINE);
			}
		}else {
			return new BlackIpExecution(BlackIpStateEnum.OFFLINE);
		}
	}

	@Override
	public BlackIpExecution getBlackIpListByPage(BlackIp blackIpCondition, int pageIndex, int pageSize) {
		BlackIpExecution be = new BlackIpExecution();
		if(blackIpCondition != null) {
			//将页码转换成行码
			int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
			//根据查询条件,待用dao层返回相关类型列表
			List<BlackIp> blackIpList = blackDao.queryBlackIpListByPage(blackIpCondition, rowIndex, pageSize);
			int count = blackDao.queryBlackIpListCount(blackIpCondition);
			
			if(blackIpList != null && count != 0) {
				be.setBlackIpList(blackIpList);
				be.setCount(count);
				be.setState(BlackIpStateEnum.SUCCESS.getState());
			}else {
				be.setState(BlackIpStateEnum.OFFLINE.getState());
			}
			
		}else {
			be.setState(BlackIpStateEnum.NULL_BLACKIP.getState());
		}
		return be;
	}

	@Override
	public BlackIpExecution getAllBlackIpCount() {
		BlackIpExecution be = new BlackIpExecution();
		Long allCount = blackDao.queryAllBlackIpCount();
		if(allCount > 0) {
			be.setAllCount(allCount);
			be.setState(BlackIpStateEnum.SUCCESS.getState());
		}else {
			be.setState(BlackIpStateEnum.OFFLINE.getState());
		}
		return be;
	}
}
